import { getStream } from '../Core/connect'
import { Object3D } from './Object3D'
// import * as MathUtils from '../Math/MathUtils'
import { Vector3 } from '../Math/Vector3'


export class Glb extends Object3D {
    readonly type: string
    constructor(name: string) {
        super({name: name, type: 'Glb'})
        this.type = 'Glb'
    }
    setUrl(url: string) {
        const that = this
         return new Promise(function (resolve) {
            const stream = getStream()
            const str = {glbFunction: 'setGlbUrl', ids: `${that.uuid}`, url:`${url}`}
            stream.emitUIInteraction(str)         
            const getGlb = (data:any) => {
                console.log('回调')
                const returnData = JSON.parse(data)
                if(returnData.sessionId === `${that.uuid}`){
                    console.log('加载完成')
                    stream.removeResponseEventListener(`glbResolve${that.uuid}`)
                    resolve(that)
                }
            }
            stream.addResponseEventListener(`glbResolve${that.uuid}`, getGlb)
        })
    }
    clearGlb() {
        const stream = getStream()
        const str = {glbFunction: 'clearGlb'}
        stream.emitUIInteraction(str)
    }
    StopAnimation() {
        const stream = getStream()
        const str = {glbFunction: 'StopAnimation', ids: `${this.uuid}`}
        stream.emitUIInteraction(str)
    }
    StartAnimation() {
        const stream = getStream()
        const str = {glbFunction: 'StartAnimation', ids: `${this.uuid}`}
        stream.emitUIInteraction(str)
    }
    setLongLatPosition(position:Vector3){
        this.position = position
        const stream = getStream()
        const str = {glbFunction: 'setLonLatPosition', ids: `${this.uuid}`,position:`${this.position.x},${this.position.y},${this.position.z}`}
        stream.emitUIInteraction(str)
    }
    getGlbByName(name: string) {
        const that = this
        return new Promise(function (resolve) {
            const stream = getStream()
            const str = {glbFunction: 'getGlbByName', ids: `${name}`}
            stream.emitUIInteraction(str)
            const getGlbPosition = (data: any) => {
                const returnData = JSON.parse(data)
                console.log('模型位置data',returnData)
                if(returnData.function === 'glbPosition'){
                    stream.removeResponseEventListener(`glbPosition${that.uuid}`)
                    resolve(returnData)
                }
            }
            stream.addResponseEventListener(`glbPosition${that.uuid}`, getGlbPosition)
        })
    }
    setClick(click: string) {
        const stream = getStream()
        const str = {glbFunction: 'startGlbClick', ids: `${this.uuid}`, startClick: click}
        stream.emitUIInteraction(str)
        const getGlbClick = (data: string) => {
            const returnData = JSON.parse(data)
            console.log(111,returnData)
            // if (returnData.sessionId === sessionId) {
            // }
        }
        stream.addResponseEventListener(`glbClick${this.uuid}`, getGlbClick)
    }
}